Shared Access Signatures (1 / 7): You are developing a .NET application that interacts with Azure Blob Storage. You need to generate a token that will allow a client to have read access to a specific container in your storage account for a period of 48 hours. This token should be generated in a straightforward manner, even if it involves sharing your storage account key. The client is not authenticated with Microsoft Entra ID. Write the C# code to accomplish this task.
string accountName = "<storage-account-name>";
string accountKey = "<storage-account-key>";
string containerName = "<container-name>";
// Code here
Answer:
string accountName = "<storage-account-name>";
string accountKey = "<storage-account-key>";
string containerName = "<container-name>";
var sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
var sasBuilder = new BlobSasBuilder()
{
BlobContainerName = containerName,
Resource = "c",
StartsOn = DateTimeOffset.UtcNow,
ExpiresOn = DateTimeOffset.UtcNow.AddDays(2),
Protocol = SasProtocol.Https
};
sasBuilder.SetPermissions(BlobSasPermissions.Read);
// Use the key to get the SAS token
string sasToken = sasBuilder.ToSasQueryParameters(sharedKeyCredential).ToString();